package com.wntms.ywa.service.impl;


import com.wntms.ywa.dao.AddDao;
import com.wntms.ywa.dao.TmsDao;
import com.wntms.ywa.pojo.AddNew;
import com.wntms.ywa.pojo.Aid;
import com.wntms.ywa.pojo.CustomerIn;
import com.wntms.ywa.pojo.CustomerInformation;
import com.wntms.ywa.service.AddService;
import com.wntms.ywa.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Service
@Transactional
public class AddServiceImpl implements AddService {
    @Autowired
    private AddDao addDao;
    @Autowired
    private TmsDao t;
    @Override
    public void addCustomerIn(AddNew addNew) {
        CustomerIn customerIn =new CustomerIn();
        customerIn.setTel(addNew.getRtel());
        customerIn.setName(addNew.getRname());
        customerIn.setAddress(addNew.getRaddress());
        addDao.addCustomerIn(addNew);
    }

    @Override
    public void addCustomerInformation(AddNew addNew) {
        CustomerInformation customerInformation=new CustomerInformation();
        customerInformation.setTel(addNew.getPtel());
        customerInformation.setAddress(addNew.getPaddress());
        customerInformation.setName(addNew.getPname());
        addDao.addCustomerInformation(addNew);
    }

@Override
      public Aid findMaxId( ) {
          return addDao.findMaxId();
            }
    @Override
    public void addFreightContract(AddNew addNew) {
        //        运费保险费生成
        Double num=(Double)(Math.random()*100)+100;
        BigDecimal bigDecimal = new BigDecimal(num);
        int n=(int)(Math.random()*100);
        double numm = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        addNew.setFare(numm);
        Double num2=numm+n+20.43;
        BigDecimal bigDecimal1 = new BigDecimal(num2);
        double nm = bigDecimal1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        addNew.setInsurancefare(nm);
//        状态设定
        addNew.setAuditstatus("未审核");
//        操作时间生成
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(date);
        addNew.setOccurtime(time);
//      运单编号生成
        String uuid = UUIDUtils.createUUID();
        addNew.setFid(uuid);

        Integer id=addNew.getId();
        addNew.setUid(id);
        addNew.setCoid(id);
        addNew.setFiid(id);
        addNew.setFlid(id);
        addNew.setFsid(id);
        int m=(int)((Math.ceil(Math.random()*2)));
        addNew.setRefundtype(m);
//        addNew.setSdate();
        addDao.addFreightContract(addNew);
    }

    @Override
    public void addFreightLine(AddNew addNew) {
        Date date=new Date();
        int time=(int)(Math.random()*5);
        Double distance=(Double)(Math.random()*500);
        BigDecimal bigDecimal = new BigDecimal(distance);
        double numm = bigDecimal.setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, time);// 24小时制,加天
        cal.add(Calendar.HOUR, time);// 24小时制 ,加小时
        cal.add(Calendar.MINUTE, time);// 24小时制,加分钟
        date = cal.getTime();
        String str_date = format.format(date);

        addNew.setArrivetime(str_date);
        addNew.setDistance(numm+75);
        addDao.addFreightLine(addNew);
    }

    @Override
    public void addFreightInformation(AddNew addNew) {
        String s=addNew.getPayment();
        System.out.println(11);
        Double m=Double.parseDouble(s);
        addNew.setFreightvalue(m);
        addDao.addFreightInformation(addNew);
    }

    @Override
    public void addFreightStatus(AddNew addNew) {
//    0代表选择代收 1代表没有选择代收
        Integer i;
        if(addNew.getPayment().length()>0){
            addNew.setIscollection(1);
            i = addNew.getIscollection();
        }else{
            addNew.setIscollection(0);
            i = addNew.getIscollection();
        }
//        默认状态
        addNew.setStatus(i);
        addDao.addFreightStatus(addNew);
    }

    @Override
    public void addC(AddNew addNew) {
//        string转化integer
        String payment = addNew.getPayment();
        Integer i=null;
        if(payment != null){
         i=Integer.valueOf(payment);
        }
        addNew.setAfter_receivables(i);
        t.addC(addNew);
    }
}
